const computedBehavior = require('miniprogram-computed')
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    switches: [
      {
        text: '普通牌照',
      },
      {
        text: '新能源'
      }
    ],
    currentIndex: 0,
    numbers: '',
    keyboardShow: false,
    last: ''
  },
  computed: {
    cardNum () {
      let nums = ''
      let numbers = this.data.numbers
      if (this.data.currentIndex === 0) {
        nums = numbers.slice(0, 7)
      } else {
        nums = numbers
      }
      return nums
    },
    latest () {
      return this.data.last
    }
  },
  methods: {
    showKeyboard() {
      this.setData({
        keyboardShow: true
      })
    },
    hideKeyboard() {
      this.setData({
        keyboardShow: false
      })
    },
    fast () {
      let currentIndex = 0
      if (this.data.last.length > 7) {
        currentIndex = 1
      } 
      this.setData({
        numbers: this.data.last,
        currentIndex
      })
    },
    clear () {
      this.data.numbers = ''
      this.setData({
        numbers: this.data.numbers
      })
    },
    changeHandler(e) {
      let currentIndex = parseInt(e.detail.value)
      this.setData({
        currentIndex
      })
    },
    numberChange(e) {
      let value = e.detail
      this.data.numbers = value.map(item => item === '' ? ' ' : item).join('')
      this.setData({
        numbers: this.data.numbers
      })
    },
    goPark () {
      let numbers = this.data.numbers
      let index = this.data.currentIndex
      if (numbers === '') {
        app.toast('请输入车牌号')
        return
      }
      if (numbers) {
        const pass = (index < 1 && numbers.trim().length === 7) ||
          (index > 0 && numbers.trim().length === 8)
        if (!pass) {
          app.toast('请输入完整车牌号')
          return
        }
      }
      wx.setStorageSync('latestParkNum', numbers)
      wx.navigateTo({
        url: `/pages/findcar/index?num=${numbers}`
      })
    },
    query () {
      let numbers = this.data.numbers
      let index = this.data.currentIndex
      if (numbers === '') {
        app.toast('请输入车牌号')
        return
      }
      if (numbers) {
        const pass = (index < 1 && numbers.trim().length === 7) ||
                    (index > 0 && numbers.trim().length === 8)
        if (!pass) {
          app.toast('请输入完整车牌号')
          return
        }
      }
      wx.setStorageSync('latestParkNum', numbers)
      wx.navigateTo({
        url: `result?num=${numbers}`
      })
    },
    onLoad(options) { },
    onReady() { },
    onShow() {
      app.memberReadyCb = () => {
      }
      app.fansReadyCb = () => {
        // app.checkMember()
      }
      app.init()
      let last = wx.getStorageSync('latestParkNum')
      this.setData({
        last
      })
    },
    onShareAppMessage() {
      return app.shareInfo
    }
  }
})